﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;

namespace InfiniDrive
{
    public partial class MainWindow : Form
    {
        private Shell32.Shell m_Shell;
        private Shell32.Folder m_RootShell;
        private Shell32.Folder m_currentFolder;
        private Icon m_IconFolder;
        private Icon infiniDriveIcon;
        //private string m_previousfilename;
        
        public MainWindow()
        {
            InitializeComponent();

            m_Shell = new Shell32.ShellClass();
            m_RootShell = m_Shell.NameSpace(Shell32.ShellSpecialFolderConstants.ssfDRIVES);

            InitializeIconFolder();
            FillLocalView(m_RootShell);

            // Remote Virtual FS 
            VirtualFSAgent vFS = new VirtualFSAgent();
            // infiniDriveIcon = new Icon("D:\\UserData\\My Documents\\EE\\semester 6\\044167 - Project A\\svn.InfiniDrive\\GraphicsInfiniDriveLogoTrans.ico");
            // FillRemoteView(vFS.VFS);


        }

        private void FillRemoteView(INode<VirtualFSInfo> node)
        { 
            this.Cursor = Cursors.WaitCursor;
           
            // Notify that update begins
            lvRemote.BeginUpdate();

            // Erase last view items
            lvRemote.Items.Clear();

            // Erase previous lists image
            RemoteImgListViewSmall.Images.Clear();
            RemoteImgListViewLarge.Images.Clear();

            int idImage = 0;

            if (node.IsRoot)
            {
                // RemoteImgListViewSmall.Images.Add(infiniDriveIcon);
                // ListViewItem RmItem = new ListViewItem(node.Data.FileName, idImage);
                ListViewItem RmItem = new ListViewItem(node.Child.Data.FileName);
                RmItem.Tag = node;
                lvRemote.Items.Add(RmItem);
            }
            else
            {
                ListViewItem RmItem = new ListViewItem("..");
                RmItem.Tag = node.Parent;

                lvRemote.Items.Add(RmItem);

                IEnumerableCollectionPair<VirtualFSInfo> items = node.AllChildren;

                // Folder enumeration
                foreach (VirtualFSInfo item in items.Values)
                {
                    if (item.IsFolder)
                    {
                        AddViewItem(item, ref idImage);
                    }
                }

                // Other files 
                foreach (VirtualFSInfo item in items.Values)
                {
                    if (!item.IsFolder)
                    {
                        AddViewItem(item, ref idImage);
                    }
                }

            } // if else 
            // End update view
            lvRemote.EndUpdate();

            //ftpc.LocalFolder = folder.Title;

            this.Cursor = Cursors.Default;
        }

        private void FillLocalView(Shell32.Folder folder)
        {

            this.Cursor = Cursors.WaitCursor;

            m_currentFolder = folder;
            // Notify that update begins
            lvLocal.BeginUpdate();

            // Erase last view items
            lvLocal.Items.Clear();

            // Erase previous lists image
            LocalImgListViewSmall.Images.Clear();
            LocalImgListViewLarge.Images.Clear();

            int idImage = 0;

            ListViewItem lvItem = new ListViewItem("..");
            lvItem.Tag = folder;

            lvLocal.Items.Add(lvItem);

            Shell32.FolderItems items = folder.Items();

            // Folder enumeration
            foreach (Shell32.FolderItem item in items)
            {
                if (item.IsFolder)
                {
                    AddViewItem(item, ref idImage);
                }
            }

            // Other files 
            foreach (Shell32.FolderItem item in items)
            {
                if (!item.IsFolder)
                {
                    AddViewItem(item, ref idImage);
                }
            }

            // End update view
            lvLocal.EndUpdate();

            //ftpc.LocalFolder = folder.Title;

            this.Cursor = Cursors.Default;

        }

        // Remote
        private void AddViewItem (VirtualFSInfo item, ref int idImage)
        {
            string[] sValues = new string[10];
            sValues[0] = item.FileName;
            if (item.FileSize == 0)
                sValues[1] = "";
            else
                sValues[1] = Convert.ToString(item.FileSize / 1024) + " KB";
            sValues[2] = item.FileExtension;
            sValues[3] = item.ModifiedTime.ToString();
            /*
            sValues[4] = item.Path;
            sValues[5] = item.IsBrowsable.ToString();
            sValues[6] = item.IsFileSystem.ToString();
            sValues[7] = item.IsFolder.ToString();
            sValues[8] = item.IsLink.ToString();
            */
            
            // LocalImgListViewSmall.Images.Add(ExtractIcon.GetIcon(item.Path, true));

            ListViewItem RmItem = new ListViewItem(sValues, idImage++);
            RmItem.Tag = item;
            lvRemote.Items.Add(RmItem);
        }

        // Local 
        private void AddViewItem(Shell32.FolderItem item, ref int idImage)
        {
            string[] sValues = new string[10];

            sValues[0] = item.Name;
            if (item.Size == 0)
                sValues[1] = "";
            else
                sValues[1] = Convert.ToString(item.Size / 1024) + " KB";
            sValues[2] = item.Type;
            sValues[3] = item.ModifyDate.ToString();
            /*
            sValues[4] = item.Path;
            sValues[5] = item.IsBrowsable.ToString();
            sValues[6] = item.IsFileSystem.ToString();
            sValues[7] = item.IsFolder.ToString();
            sValues[8] = item.IsLink.ToString();
            */
            
            LocalImgListViewSmall.Images.Add(ExtractIcon.GetIcon(item.Path, true));
            //Icon ico = IconHandler.IconFromFile(item.Path, IconSize.Small,1);
            //LocalImgListViewSmall.Images.Add(ico);

            ListViewItem lvItem = new ListViewItem(sValues, idImage++);
            lvItem.Tag = item;
            lvLocal.Items.Add(lvItem);
        }

        private void lvLocal_ItemActivate(object sender, System.EventArgs e)
        {
            if (lvLocal.SelectedItems[0].Text == "..")
            {
                Shell32.Folder item;

                item = (Shell32.Folder) lvLocal.SelectedItems[0].Tag;
                FillLocalView((Shell32.Folder) item.ParentFolder);
            }
            else
            {
                Shell32.FolderItem item;

                item = (Shell32.FolderItem)lvLocal.SelectedItems[0].Tag;
                if (item.IsFolder)
                {
                    //ftpc.LocalFolder = item.Path;
                    FillLocalView((Shell32.Folder)item.GetFolder);
                }
            }
        }

        private void lvRemote_ItemActivate(object sender, System.EventArgs e)
        {
            if (lvRemote.SelectedItems[0].Text == "..")
            {
                INode<VirtualFSInfo> item;

                item = (INode<VirtualFSInfo>)lvLocal.SelectedItems[0].Tag;
                FillRemoteView((INode<VirtualFSInfo>)item.Parent);
            }
            else
            {
                INode<VirtualFSInfo> item;

                item = (INode<VirtualFSInfo>)lvRemote.SelectedItems[0].Tag;
                item = item.Child;
                if (item.Data.IsFolder)
                {
                    FillRemoteView((INode<VirtualFSInfo>)item);
                }
            }
        }
        private void InitializeIconFolder()
        {
            Shell32.Folder FolderShell = m_Shell.NameSpace(Shell32.ShellSpecialFolderConstants.ssfWINDOWS);
            Shell32.FolderItems items = FolderShell.Items();

            foreach (Shell32.FolderItem item in items)
                if (item.IsFolder)
                {
                    m_IconFolder = ExtractIcon.GetIcon(item.Path, true);
                    break;
                }
        }
        
        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void splitContainer4_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer2_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void toolsLocalLabel_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void toolStripLabel2_Click(object sender, EventArgs e)
        {

        }

        private void toolStripLabel3_Click(object sender, EventArgs e)
        {

        }

        private void toolStripLabel4_Click(object sender, EventArgs e)
        {

        }
        
        private void btnToolsMain_AccountMng_Click(object sender, EventArgs e)
        {
            AccountMng wfAccountMng = new AccountMng();
            wfAccountMng.ShowDialog();
        }
        
        private void toolStripContainer1_ContentPanel_Load(object sender, EventArgs e)
        {

        }

        private void toolStripContainer1_TopToolStripPanel_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void toolsLocalFunc_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lvLocal_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {

        }

        private void lbStatbar_ConnStat_Click(object sender, EventArgs e)
        {

        }
        
        private void tbLocal_Path_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            tbLocal_Path.Text = folderBrowserDialog1.SelectedPath;
            // AccountMng wfAccountMng = new AccountMng();
            // wfAccountMng.ShowDialog();
        }
    }
}
